<template>
  <el-form :model="formModel" :rules="rules" ref="form" size="large" autocomplete="off">
    <el-form-item>
        <h2>注册</h2>
    </el-form-item>
    <el-form-item prop="username">
      <el-input v-model="formModel.username" :prefix-icon="User" placeholder="请输入用户名"></el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input v-model="formModel.password" :prefix-icon="Lock" type="password" placeholder="请输入密码"></el-input>
    </el-form-item>
    <el-form-item prop="repassword">
      <el-input v-model="formModel.repassword" :prefix-icon="Lock" type="password" placeholder="请再次输入密码"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button class="button" @click="register" type="info" auto-insert-space>注册</el-button>
    </el-form-item>
    <el-form-item class="flex">
      <el-link type="info" :underline="false" @click="$emit('close')">取消</el-link>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import { User, Lock } from '@element-plus/icons-vue';

// 在顶部添加 emit 声明
const emit = defineEmits(['close']) // 声明自定义事件

const form = ref();
const formModel = ref({
  username: '',
  password: '',
  repassword: ''
});

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, max: 10, message: '用户名必须是5-10位的字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是6-15位的非空字符',
      trigger: 'blur'
    }
  ],
  repassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是6-15的非空字符',
      trigger: 'blur'
    },
    {
      validator: (rule, value, callback) => {
        if (value !== formModel.value.password) {
          callback(new Error('两次输入密码不一致!'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
};

const initLocalUsers = () => {
  if (!localStorage.getItem('users')) {
    localStorage.setItem('users', JSON.stringify([
      { username: 'admin', password: '123456' }
    ]))
  }
};

onMounted(initLocalUsers);

const register = async () => {
  await form.value.validate();
  const newUser = {
    username: formModel.value.username,
    password: formModel.value.password
  };
  const users = JSON.parse(localStorage.getItem('users'));
  if (users.some(user => user.username === newUser.username)) {
    return ElMessage.error('用户名已存在');
  }
  users.push(newUser);
  localStorage.setItem('users', JSON.stringify(users));
  ElMessage.success('注册成功');
  emit('close'); // 关闭对话框
};
</script>

<style scoped>
.el-form {
  padding-top: 40px;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  width: 100%;
}

.el-form-item {
  width: 100%;
  margin-bottom: 20px;
  
}

.el-form-item:last-child {
  margin-bottom: 0;
}

.el-input {
  width: 100%;
}

.el-button {
  width: 100%;
}

.flex {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.el-checkbox {
  margin-right: 10px;
}
h2 {
    margin: 0 auto;
}
</style>